package com.alipay.util;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;


public class AESUtils {
	private String iv = "fedcba9876543210";// Dummy iv (CHANGE IT!)
	private IvParameterSpec ivspec;
	private SecretKeySpec keyspec;
	private Cipher cipher;

	private String SecretKey = "0123456789abcdef";// Dummy secretKey (CHANGE
													// IT!)

	public AESUtils(String key) {
		ivspec = new IvParameterSpec(iv.getBytes());
		if (!key.isEmpty()) {
			SecretKey = key;
		}
		keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
		try {
			cipher = Cipher.getInstance("AES/CBC/NoPadding");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}
	}

	public byte[] encrypt(String text) throws Exception {
		if (text == null || text.length() == 0)
			throw new Exception("Empty string");

		byte[] encrypted = null;

		try {
			cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);

			encrypted = cipher.doFinal(padString(text).getBytes("UTF-8"));
			encrypted = bytesToHex(encrypted).getBytes();
		} catch (Exception e) {
			e.printStackTrace();
			// throw new Exception("[encrypt] " + e.getMessage());
		}

		return encrypted;
	}

	public byte[] decrypt(String code) throws Exception {
		if (code == null || code.length() == 0)
			throw new Exception("Empty string");

		byte[] decrypted = null;

		try {
			cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

			decrypted = cipher.doFinal(hexToBytes(code));
		} catch (Exception e) {
			throw new Exception("[decrypt] " + e.getMessage());
		}
		return decrypted;
	}

	public static String bytesToHex(byte[] data) {
		if (data == null) {
			return null;
		}

		int len = data.length;
		String str = "";
		for (int i = 0; i < len; i++) {
			if ((data[i] & 0xFF) < 16)
				str = str + "0" + Integer.toHexString(data[i] & 0xFF);
			else
				str = str + Integer.toHexString(data[i] & 0xFF);
		}
		return str;
	}

	public static byte[] hexToBytes(String str) {
		if (str == null) {
			return null;
		} else if (str.length() < 2) {
			return null;
		} else {
			int len = str.length() / 2;
			byte[] buffer = new byte[len];
			for (int i = 0; i < len; i++) {
				buffer[i] = (byte) Integer.parseInt(str.substring(i * 2,
						i * 2 + 2), 16);
			}
			return buffer;
		}
	}

	private static String padString(String source) {
		char paddingChar = ' ';
		int size = 16;
		int x = source.length() % size;
		int padLength = size - x;

		for (int i = 0; i < padLength; i++) {
			source += paddingChar;
		}

		return source;
	}
	
	public static void main(String[] args) {
		AESUtils a = new AESUtils("PLZxply260817412");
		try {
//			System.out.println(new String(a.encrypt("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCWMtJ0dAmAQqWCrCZraBeLGCFMjuhCQc7xAvZe TO0GOKPCc5wdjwZ8yovPb/0OBcRivmiI1hcv2w8wDT8ja7YOJlZtjjujEKPOzFiMjbkq9JoI/+t6 FsjEpeTOVTEldSLeYpy6KdW1bxAPa6R7de+C6HfMK4NfHKnItNrFdG5L5QIDAQAB")));//公钥
			System.out.println(new String(a.encrypt("MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALo87MeVFrznAk4AGBfH6NgxuP7tYMXW6QvGn59BMEn0tsIrXvVMP/dgAlocc0bP92kgd1GY7S3m3VNVNCMcqQfrq1salUMnA9XWBSUD09SpUs66eGzMANG+M3I/MXv5F9WmKNTHHP2NkynqUx2V7nxoy3dRDfo+orwfrGjpzca9AgMBAAECgYAwNbdBjvl82vwAvwt9lHVVCcIO09oaPKDFOJx3eEOM0XlB2EJTk3J3kfXp57V7njg4kXvxtnmnSTz/5m2BrVif0B2YYGAg/9zcs9p3WL9VL6OuQbCjIaG3jj/MUb9/0nQaD9LF7m2iDq2A5esAS3s7y2Jualrpx3jmAjA3xJ48bQJBAOQMGLWk7sVQsn/N5wolhV8fyH0x5Kk60kTGQSOcOS2pE2X3lKUZkK0R5GUTd5mbZnhCXMJPJPBqHCDmC20EqX8CQQDREOTWYICQoaPsCm/rbQBuNK+SkK+q04Aldgai9Qr+Mjm38sB8BByzHwcWVuYQrD9pkXOTeoyoMSXyZdIaxtXDAkEAhQchauSZfHTEi5U7D0ov8PuDYrS2JJnxirjzyqgZGq+kZzjzdxPgt3TWfLfMbsieD8z2e0rby3q6id5oD+JGVQJBALrVpiwPQdX/HkcrbKmwV9olK2KH+vVm3faca4HmC1Tv/bTuNCagZiWNbQAB2Mi9pk3ptqfttRX6Fjdx1zomx5ECQHg+1u35cGv56X9U5ndj9PSwP1AlcI8jzxt+FbS4HHH8Q6xKvjGjEDoBKvzRwm56sSuHIhezcD9eF130Ars+5/Q=")));//秘钥
//			System.out.println(new String(a.encrypt("oovf3kgj8awmy2ulfulxhvbejrkdnxgp")));//MD5秘钥
//			System.out.println(new String(a.encrypt("2088021230434146")));//合作者ID
			System.out.println(new String(a.decrypt("0a3901c2acd5cf131905d9132cc4d68cf7eab90a65706198facd61fbadc19c7f066b07442f3c3ba8a0851ef5efcfbfb79aebf39fbbd8cfd63233ea3445826ccfb9413d50a38440119c04adb740a07e7900da0d812991c3593bdbc7a4ff5c366e10624cd079b3f8cd5649475c5a80ea859dacc142ce119d7120616860472d9ce29b7eb41a1e02d8f77115646b28ec4b914a11776de5a1f69393d52269238793a3c4694d3820de6655951463a893e1f84eca95d07e3e09f3c7da14de0b356a574fea8ddc7f4513745660deeecebdb83bc05b20e33113c3ba1b674d5ee53f5a61a321c517ac6537522f340bb9e8d68ca5705b56cfd0f1171ad2323f84600c1fd76d708505e0e04027ea0ae3203c0522c0ec90e8ef33e14950e6fc8a7c449fad70812968f629a171548e19ae648c968fa319dfe5ae42b8ba18e43403af96036a05ee8c00be3079b03191c8f39553c867d161eb6e3de3fb768c874fee22333be8cadf911b2e51dd9aa9b18ca726ffdb96516f6e7801681efe5ee1a743001cd142754e30af8440956daf418504eaa49ba6afa5bfe4d3db2b8d056a666fcff0bd518ccdf6acc80364d4e43dd641f191c5b33a8e723cc7426fac35097362cc3d48d5ca377488a2c744e632b5302f514d11d78342993c50738cac7520a793a1ef1abd7f290c497f07fc7dd0177a9bb3990f32d795d2240d632d5d9acf6c9cab93da0312f58d3e3596c79eadd6b360d34b6316e112bd2b6bd3429f1b49ad5497f69ece34abe3f1a389b622c416e8d7c42dbccae8fd71d1dd53e0e57cb695d6313149c2caa9cebdf151d94acf207a78e02a3510025d192c1ddf7e50ef815bcc149ae4edee21db9dad9478d058b55e97bf353282250ff21c8cfa817fff8bcc90ceffb958e2bc8ee2d4aa1a116b3ad9aa5498a1adb144761eba9fecb8b47e2ddefd1c929baa853711a8a2e6bfd528031af9675b85bffc0240a3be1565d29a0aba78a6cf6214647168f3dbbbe90535c711665db21b8b26e75ced055bc922a767f276bb89c7dc44ff47c6c1787d2caa48202319842745ca93a2d377f00ea654eb9a71173d863f9e2c0854ad9758704874a69275bc969747943891756dde5fb5ab03b7f304dc6d73aaa111a9febb54e999a9967564c23827")));
//			System.out.println(util.MD5Sign("SUCCESS000000SUCCESSMD5OfflineResult1.0800077100050001201412240002648157100151****2454M20150325100000040102015032614591820150326151331201412240", "1wYt0LJNBujV7B0XmXQsMAstIskqk81a4jCqxm1x529mU2A8MSsiLIKVqVpSBgrq"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
